package com.cvticketvender;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import java.util.List;

import cv.vender.YtMainBoard;
import cv.vender.clsCmdConst;
import cv.vender.para.clsADCPara;
import cv.vender.para.clsCalibrationPara;
import cv.vender.para.clsCardSnPara;
import cv.vender.para.clsCrossPara;
import cv.vender.para.clsHandShakePara;
import cv.vender.para.clsPcPollCreditPara;
import cv.vender.para.clsPcPollPara;
import cv.vender.para.clsPcSubCreditPara;
import cv.vender.para.clsPollTransfor;
import cv.vender.para.clsRealValPara;
import cv.vender.para.clsSetCashDevicePara;
import cv.vender.para.clsSetRelayPara;
import cv.vender.para.clsTradeMsgToMcu;
import cv.vender.para.clsTradeTypeStatistic;
import cv.vender.para.clsTransforPara;


public class ThreadForBrd extends Thread {


    private static ThreadForBrd ins = null;
    private String commid = null;
    private int baudrate = 0;
    private Handler mChildHandler;
    private Handler mMainHandler;
    private Handler mMainHandlerBak;

    public static void OpenIns(String commid, int baudrate) {
        ins.baudrate = baudrate;
        ins.commid = commid;
        YtMainBoard.getInstance().EF_OpenDev(ins.commid, ins.baudrate);
    }

    public static ThreadForBrd getInstence() {
        if (ins == null) {
            ins = new ThreadForBrd();
        }
        return ins;
    }

    public String getCommid() {
        return commid;
    }

    public void setCommid(String commid) {
        this.commid = commid;
    }

    public int getBaudrate() {
        return baudrate;
    }

    public void setBaudrate(int baudrate) {
        this.baudrate = baudrate;
    }

    Handler getmChildHandler() {
        return mChildHandler;
    }

    void setmMainHandler(Handler mMainHandler) {
        this.mMainHandler = mMainHandler;
    }

    @SuppressLint("HandlerLeak")
    public void run() {
        Looper.prepare();

        mChildHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                try {
                    switch (msg.what) {
                        case clsCmdConst.CMD_OPEN_SERIAL_PORT://打开串口

                            break;

                        case clsCmdConst.CMD_PC_LNK://握手命令
                            MainThreadLink();
                            break;
                        case clsCmdConst.CMD_PC_POLL://查询主板状态命令(有使用)
                            MainThreadPoll();
                                /*启动下一次poll*/
                            //mChildHandler.sendEmptyMessageDelayed(clsCmdConst.CMD_PC_POLL, 500);
                            break;

                        case clsCmdConst.CMD_PC_CREDIT: {  //信息命令
                            clsPcPollCreditPara para = (clsPcPollCreditPara) msg.obj;
                            int ret = YtMainBoard.getInstance().EF_PcPollCredit(para);
                            Message msg2 = mMainHandler.obtainMessage(msg.what, para);
                            msg2.arg1 = ret;
                            msg2.sendToTarget();
                        }
                        break;
                        case clsCmdConst.CMD_PC_MDB_CLEAR_CREDIT: {  //清除数据库信用命令
                            clsPcPollCreditPara para = (clsPcPollCreditPara) msg.obj;
                            int ret = YtMainBoard.getInstance().EF_PcClrCredit(para);
                            Message msg2 = mMainHandler.obtainMessage(msg.what, para);
                            msg2.arg1 = ret;
                            msg2.sendToTarget();
                        }
                        break;

                        case clsCmdConst.CMD_PC_GET_CALIBRATION:

                            break;

                        case clsCmdConst.CMD_PC_POLL_TRANSFOR: { //小票改变命令
                            clsPollTransfor para = (clsPollTransfor) msg.obj;
                            if (para == null) {
                                para = new clsPollTransfor();
                            }
                            int ret = YtMainBoard.getInstance().EF_PollTransfor(para);
                            Message msg2 = mMainHandler.obtainMessage(msg.what, para);
                            msg2.arg1 = ret;
                            msg2.sendToTarget();
                        }
                        break;

                        case clsCmdConst.CMD_PC_READ_ADC: {  //读取机器信息的命令
                            clsADCPara para = (clsADCPara) msg.obj;
                            int ret = YtMainBoard.getInstance().EF_GetAdcVal(para);
                            Message msg2 = mMainHandler.obtainMessage(msg.what, para);
                            msg2.arg1 = ret;
                            msg2.sendToTarget();
                        }
                        break;

                        case clsCmdConst.CMD_PC_GET_WEIGHT: { //读取实际称量
                            clsRealValPara para = (clsRealValPara) msg.obj;
                            int ret = YtMainBoard.getInstance().EF_GetRealVal(para);
                            Message msg2 = mMainHandler.obtainMessage(msg.what, para);
                            msg2.arg1 = ret;
                            msg2.sendToTarget();
                        }
                        break;
                        case clsCmdConst.CMD_PC_RELAY_TEST: {  //设置继电器
                            clsSetRelayPara para = (clsSetRelayPara) msg.obj;
                            int ret = YtMainBoard.getInstance().EF_SetRelay(para);
                            Message msg2 = mMainHandler.obtainMessage(msg.what, para);
                            msg2.arg1 = ret;
                            msg2.sendToTarget();
                        }
                        break;

                        case clsCmdConst.CMD_PC_SET_BILL_TYPE: {
                            clsSetCashDevicePara para = (clsSetCashDevicePara) msg.obj;
                            int ret = YtMainBoard.getInstance().EF_SetSetCashDevice(para);
                            Message msg2 = mMainHandler.obtainMessage(msg.what, para);
                            msg2.arg1 = ret;
                            msg2.sendToTarget();
                        }
                        break;

                        case clsCmdConst.CMD_PC_SET_CALIBRATION: {
                            List<clsCalibrationPara> para = (List<clsCalibrationPara>) msg.obj;
                            int ret = YtMainBoard.getInstance().EF_SetCalibration(para);
                            Message msg2 = mMainHandler.obtainMessage(msg.what, para);
                            msg2.arg1 = ret;
                            msg2.sendToTarget();
                        }
                        break;
                        case clsCmdConst.CMD_PC_TRANSFOR: {
                            clsTransforPara para = (clsTransforPara) msg.obj;
                            int ret = YtMainBoard.getInstance().EF_DoTransfor(para);
                            Message msg2 = mMainHandler.obtainMessage(msg.what, para);
                            msg2.arg1 = ret;
                            msg2.sendToTarget();
                        }
                        break;
                        case clsCmdConst.CMD_PC_GET_GROSS: {
                            clsCrossPara para = (clsCrossPara) msg.obj;
                            int ret = YtMainBoard.getInstance().EF_GetCrossVal(para);
                            Message msg2 = mMainHandler.obtainMessage(msg.what, para);
                            msg2.arg1 = ret;
                            msg2.sendToTarget();
                        }
                        break;

                        case clsCmdConst.CMD_PC_GET_CARDSN: {
                            clsCardSnPara para = new clsCardSnPara();
                            int ret = YtMainBoard.getInstance().EF_GetCardSn(para);
                            Message msg2 = mMainHandler.obtainMessage(msg.what, para);
                            msg2.arg1 = ret;
                            msg2.sendToTarget();
                        }
                        break;
                        case clsCmdConst.CMD_PC_MDB_SUB_CREDIT: {
                            clsPcSubCreditPara para = (clsPcSubCreditPara) msg.obj;
                            int ret = YtMainBoard.getInstance().EF_PcSubCredit(para);
                            Message msg2 = mMainHandler.obtainMessage(msg.what, para);
                            msg2.arg1 = ret;
                            msg2.sendToTarget();
                        }
                        break;

                        case clsCmdConst.CMD_PC_LOAD_TRADE_MSG: {
                            clsTradeMsgToMcu para = new clsTradeMsgToMcu();
                            para.setTotal(new clsTradeTypeStatistic());
                            para.setCash(new clsTradeTypeStatistic());
                            para.setWeixin(new clsTradeTypeStatistic());
                            para.setAlipay(new clsTradeTypeStatistic());
                            int ret = YtMainBoard.getInstance().EF_LoadTradeMsg(para);
                            Message msg2 = mMainHandler.obtainMessage(msg.what, para);
                            msg2.obj = para;
                            msg2.arg1 = ret;
                            msg2.sendToTarget();
                        }
                        break;
                        case clsCmdConst.CMD_PC_SAVE_TRADE_MSG: {
                            clsTradeMsgToMcu para = (clsTradeMsgToMcu) msg.obj;
                            int ret = YtMainBoard.getInstance().EF_SaveTradeMsg(para);
                            Message msg2 = mMainHandler.obtainMessage(msg.what, para);
                            msg2.arg1 = ret;
                            msg2.sendToTarget();
                        }
                        break;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }


            }


            /**
             *
             */
            private void MainThreadPoll() {
                clsPcPollPara para = new clsPcPollPara();
                int ret = YtMainBoard.getInstance().EF_PcPoll(para);
                Message msg = mMainHandler.obtainMessage(clsCmdConst.CMD_PC_POLL, para);
                msg.arg1 = ret;
                msg.sendToTarget();
            }

            /**
             * 与主板握手
             */
            private void MainThreadLink() {
                clsHandShakePara para = new clsHandShakePara();
                int ret = YtMainBoard.getInstance().EF_HandShake(para);
                Message msg = mMainHandler.obtainMessage(clsCmdConst.CMD_PC_LNK, para);
                msg.arg1 = ret;
                msg.sendToTarget();
            }
        };

        //setmChildHandler(mChildHandler);
            /*启动完成*/
        //mChildHandler.sendEmptyMessageDelayed(clsCmdConst.CMD_PC_POLL, 1500);

        SendPcLnkRequest();

        Message tomain = mMainHandler.obtainMessage(clsCmdConst.CMD_START_OK);
        tomain.sendToTarget();
        Looper.loop();
    }

    public void toDefaultState() {
        mMainHandler = mMainHandlerBak;
        mMainHandlerBak = null;
    }

    public void toSetState(Handler sethandle) {
        mMainHandlerBak = mMainHandler;
        mMainHandler = sethandle;
    }

    public void SetCashType(int bills, int coins) {
        clsSetCashDevicePara para = new clsSetCashDevicePara();
        para.setBills(bills);
        para.setCoins(coins);

        Message msg = null;
        msg = getmChildHandler().
                obtainMessage(clsCmdConst.CMD_PC_SET_BILL_TYPE, para);
        msg.sendToTarget();
    }

    public void StartTransfor(int g_amount) {
        clsTransforPara para = new clsTransforPara();
        para.setRiceAmount(g_amount);
        Message msg = null;
        msg = getmChildHandler().obtainMessage(clsCmdConst.CMD_PC_TRANSFOR, para);
        msg.sendToTarget();
    }

    public void SendPcLnkRequest() {
        mChildHandler.sendEmptyMessageDelayed(clsCmdConst.CMD_PC_LNK, 500);
    }

    public void SendPollTransforRequest() {
        mChildHandler.sendEmptyMessageDelayed(clsCmdConst.CMD_PC_POLL_TRANSFOR, 500);
    }

    public void StartCreditPoll() {
        clsPcPollCreditPara para = new clsPcPollCreditPara();
        Message msg = mChildHandler.obtainMessage(clsCmdConst.CMD_PC_CREDIT, para);
        mChildHandler.sendMessageDelayed(msg, 200);
    }

    public void StartGetCardSn() {
        Message msg = mChildHandler.obtainMessage(clsCmdConst.CMD_PC_GET_CARDSN);
        msg.sendToTarget();
    }

    public void ClearAllCredit() {
        clsPcPollCreditPara para = new clsPcPollCreditPara();
        Message msg = null;
        msg = mChildHandler.obtainMessage(clsCmdConst.CMD_PC_MDB_CLEAR_CREDIT, para);
        mChildHandler.sendMessageDelayed(msg, 200);
    }

    public void StartPcPoll() {
        Message msg = mChildHandler.obtainMessage(clsCmdConst.CMD_PC_POLL);
        mChildHandler.sendMessageDelayed(msg, 200);
    }

    public void SubSomeCredit(int cashAmount) {
        clsPcSubCreditPara para = new clsPcSubCreditPara();
        Message msg = null;
        para.setSubCredit(cashAmount);
        msg = mChildHandler.obtainMessage(clsCmdConst.CMD_PC_MDB_SUB_CREDIT, para);
        msg.sendToTarget();
    }


    public void GetTradeMsg() {
        Message msg = mChildHandler.obtainMessage(clsCmdConst.CMD_PC_LOAD_TRADE_MSG);
        msg.sendToTarget();
    }

    public void SetTradeMsg(clsTradeMsgToMcu para2) {
        Message msg = mChildHandler.obtainMessage(clsCmdConst.CMD_PC_SAVE_TRADE_MSG);

        clsTradeMsgToMcu para = new clsTradeMsgToMcu();
        para.setXuhao(para2.getXuhao());
        para.setTotal(new clsTradeTypeStatistic());
        para.setCash(new clsTradeTypeStatistic());
        para.setWeixin(new clsTradeTypeStatistic());
        para.setAlipay(new clsTradeTypeStatistic());

        para.getTotal().CopyObj(para2.getTotal());
        para.getCash().CopyObj(para2.getCash());
        para.getWeixin().CopyObj(para2.getWeixin());
        para.getAlipay().CopyObj(para2.getAlipay());

        para.setStart_timestamp(para2.getStart_timestamp());
        para.setEnd_timestamp(para2.getEnd_timestamp());

        msg.obj = para;
        msg.sendToTarget();
    }
}
